package com.hxl.demo.service.Impl;

import com.hxl.demo.entity.SocialSecurityCard;
import com.hxl.demo.mapper.SocialSecurityCardMapper;
import com.hxl.demo.mapper.StaffMapper;
import com.hxl.demo.service.SocialSecurityCardService;
import com.hxl.demo.tools.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class SocialSecurityCardServiceImpl implements SocialSecurityCardService {
    @Resource
    private SocialSecurityCardMapper socialSecurityCardMapper;
    @Resource
    private StaffMapper staffMapper;
    @Override
    public PageInfo<SocialSecurityCard> selectAll(int begin, int length, SocialSecurityCard socialSecurityCard) {
        PageInfo<SocialSecurityCard> pageInfo = new PageInfo<>();
        pageInfo.setTotal(socialSecurityCardMapper.getCount(socialSecurityCard));
        Map<String, Object> map = new HashMap<>();
        map.put("begin", begin);
        map.put("length", length);
        map.put("social", socialSecurityCard);
        pageInfo.setData(socialSecurityCardMapper.selectAll(map));
        return pageInfo;
    }

    @Override
    public int updateById(SocialSecurityCard socialSecurityCard) {
        return socialSecurityCardMapper.updateById(socialSecurityCard);
    }

    @Override
    public int saveSocial(SocialSecurityCard socialSecurityCard, String tel) {
        int result = -1;
        int byCardId = socialSecurityCardMapper.getCountByCardId(socialSecurityCard.getCardId());
        if (byCardId > 0) {
            return result;
        } else {
            //通过身份证号码查询是否有此人
            int staffId = staffMapper.getStaffIdByTel(tel);
            if(staffId >= 0) {
                int byStaffId = socialSecurityCardMapper.getCountByStaffId(staffId);
                if(byStaffId > 0) {
                    return result;
                } else {
                    socialSecurityCard.setStaffId(staffId);
                    socialSecurityCard.setInsertTime(new Date());
                    socialSecurityCard.setStat("1");
                    return socialSecurityCardMapper.saveSocial(socialSecurityCard);
                }
            } else {
                return result;
            }
        }
    }
}
